package exercise;

import java.util.Comparator;
import java.util.concurrent.*;

/**
 * Created with IntelliJ IDEA
 * Description：线程池的创建方式
 * User：chenshu
 * Date：2023-11-16
 * Time：09:00
 */


public class Demo1 {
    public static void main(String[] args) {
        ExecutorService threadPool1 = Executors.newCachedThreadPool();
        ExecutorService threadPool2 = Executors.newFixedThreadPool(10);
        ExecutorService threadPool3 = Executors.newSingleThreadExecutor();
        //使用实例
        threadPool1.submit(() -> {
            System.out.println("submit a task");
        });

        ScheduledExecutorService threadPool4 = Executors.newSingleThreadScheduledExecutor();
        ScheduledExecutorService threadPool5 = Executors.newScheduledThreadPool(8);
        //使用实例
        Runnable task = () -> {
            System.out.println("this is a scheduled task");
        };
        threadPool4.schedule(task, 5, TimeUnit.MICROSECONDS);

        //不传参数，线程个数为CPU核心数
        ExecutorService threadPool6 = Executors.newWorkStealingPool();
        threadPool6.submit(() -> System.out.println("this is threadPool6"));

        ThreadPoolExecutor threadPool7 = new ThreadPoolExecutor(5, 10, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
        for (int i = 0; i < 10; i++) {
            threadPool7.submit(()-> System.out.println("hello thread!"));
        }
    }
}
